//
//  XzxyybVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/22.
//

import UIKit

class XzxyybVC: BaseVC {

    var scrollView: UIScrollView!
    
    var ypwj: LGFLoginImageTitleView!
    var xcbg: LGFLoginImageTitleView!
    var bh: LGFLoginImageTitleView!
    var jcsj: LGFLoginImageTitleView!
    var mzh: LGFLoginImageTitleView!
    var zyh: LGFLoginImageTitleView!
    
    var tw: LGFLoginImageTitleView!
    var phcd: LGFLoginImageTitleView!
    var sfzj: LGFSegmentView!
    var cjcs: LGFLoginImageTitleView!
    var xczd: LGFLoginImageTitleView!
    var lczd: LGFLoginImageTitleView!
    var xytzms: UITextView!
    var xytzms_p: UILabel!
    
    var twIdArr: [String]!
    var twArr: [String]!
    var phcdIdArr: [String]!
    var phcdArr: [String]!
    var cjcsIdArr: [String]!
    var cjcsArr: [String]!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        getData()
    }
    
    func getData() {
        let paramet: [String: Any] = [
            "groupCode": "SAMPLE_POSITION",
        ]
        EWERequest.lgf("dictionary", paramet, .get) { [weak self] (data) in
            self?.twIdArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                return (dict["id"] as? String) ?? ""
            })
            self?.twArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                return (dict["title"] as? String) ?? ""
            })
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
        let paramet2: [String: Any] = [
            "groupCode": "SAMPLE_COORDINATE ",
        ]
        EWERequest.lgf("dictionary", paramet2, .get) { [weak self] (data) in
            self?.phcdIdArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                return (dict["id"] as? String) ?? ""
            })
            self?.phcdArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                return (dict["title"] as? String) ?? ""
            })
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
        let paramet3: [String: Any] = [
            "groupCode": "SAMPLE_SITE",
        ]
        EWERequest.lgf("dictionary", paramet3, .get) { [weak self] (data) in
            self?.cjcsIdArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                return (dict["id"] as? String) ?? ""
            })
            self?.cjcsArr = (data["result"] as? [[String: Any]] ?? []).map({ (dict) -> String in
                return (dict["title"] as? String) ?? ""
            })
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }
    
    @objc func s_bc() {
        if (ypwj.lgf_Name ?? "").lgf_IsBlank() {
            SVProgressHUD.showError("请选择新增样本文件")
            return
        }
        if (tw.lgf_Name ?? "").lgf_IsBlank() {
            SVProgressHUD.showError("请选择体位")
            return
        }
        if (phcd.lgf_Name ?? "").lgf_IsBlank() {
            SVProgressHUD.showError("请选择配合程度")
            return
        }
        if (cjcs.lgf_Name ?? "").lgf_IsBlank() {
            SVProgressHUD.showError("请选择采集场所")
            return
        }
        let paramet: [String: Any] = [
            "SampleDetail": [
                "wavPath": ypwj.lgf_Name,
                "reportPath": xcbg.lgf_Name,
            ],
            "examineDate": (jcsj.textField.text ?? "") + " 00:00:00",
            "no": bh.text ?? "",
            "height": mzh.textField.text ?? "",
            "weight": zyh.textField.text ?? "",
            "position": tw.lgf_Name ?? "",
            "coordinate": phcd.lgf_Name ?? "",
            "calm": sfzj.selectIndex,
            "site": cjcs.lgf_Name ?? "",
            "hospitalId": EWEUser.lgf.hospitalId ?? "",
            "diagnose": xczd.textField.text ?? "",
            "clinicInfo": lczd.textField.text ?? "",
            "descn": xytzms.text ?? "",
        ]
        EWERequest.lgf("sample", paramet, .post, JSONEncoding.default) { (data) in
            SVProgressHUD.showSuccess("保存成功")
            Timer.lgf_After(S: 0.5) {
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ReloadWdxyybVC"), object: nil)
                self.navigationController?.popViewController(animated: true)
            }
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }
    
}
